package pay

import (
	"chatgpt/helper"
	"chatgpt/helper/code"
	"chatgpt/logq"
	"chatgpt/model/app"
	appsrv "chatgpt/services/app"
	"chatgpt/services/datasrv/sqlhelper"
	"chatgpt/services/pay/deliver"
	"github.com/slclub/go-tips"
	"strings"
)

type PaymentSrv struct {
}

// 支付发货
func (self *PaymentSrv) PayAction(data any) code.CODENO {
	logq.DebugF("PAY.PAYACTION.REQIEST  REQ:%v", data)
	result := self.Conv(data)
	third := newThidPay(result)
	if mc := third.CheckSDK(data); mc != code.SUCCESS {
		logq.InfoF("PAY.PAYMENT.SDK SDK:UNVALIDE orderno:%v ", result.SerialNo)
		return mc
	}
	// 修改订单状态 do order somthings
	orderSrv := appsrv.OrderSrv{}
	order := orderSrv.GetOrder(result.SerialNo)
	if mc := self.CheckOrder(order, result); mc != code.SUCCESS {
		logq.InfoF("PAY.PAYMENT.ORDER-CHECK orderno:%v", result.SerialNo)
		return mc
	}
	result.UUID = order.UUID
	// 第三方订单确认

	if mc := third.Confirm(result); mc != code.SUCCESS {
		logq.InfoF("PAY.PAYMENT.THIRD.CONFIRM orderno:%v", result.SerialNo)
		return mc
	}

	order.State = helper.ORDER_STATE_PAY
	order.PayNo = result.PlatNo
	order.PayType = result.PayType
	cbs := orderSrv.CallBackDecode(order.CallBack)
	cbs = append(cbs, tips.String(order.State))

	defer orderSrv.Save(order)
	defer func() { order.CallBack = orderSrv.CallBackEncode(cbs) }()
	logq.InfoF("PAY.ORDER.STATE.OVER orderno:%v", result.SerialNo)
	// 发货商品

	// 发货事件
	productSrv := appsrv.ProductSrv{}
	deliver.EventDeliver.Emit(map[string]any{
		"Proc":  productSrv.First(order.ProcID),
		"Order": order,
		"UUID":  result.UUID,
	}, self.EventErrors)

	// 花销
	self.Cost(order)
	//
	order.State = helper.ORDER_STATE_DELIVERY
	cbs = append(cbs, tips.String(order.State))
	//order.CallBack = orderSrv.CallBackEncode(cbs)
	logq.InfoF("PAY.DELIVER")
	return code.SUCCESS
}

func (self *PaymentSrv) Conv(data any) *app.PayResult {
	result := &app.PayResult{}
	if data_m, ok := data.(map[string]any); ok {
		result.UUID = tips.String(data_m["UUID"])
		result.SerialNo = tips.String(data_m["SerialNo"])
		result.PlatNo = tips.String(data_m["PlatNo"])
		result.Amount = tips.Int(data_m["Amount"])
		result.Ext = tips.String(data_m["ext"])
		result.PayType = tips.String(data_m["PayType"])
		result.Time = helper.AutoTimeStamp(data_m["PlatTime"])
		return result
	}
	return result
}

func (self *PaymentSrv) CheckOrder(order *app.Order, result *app.PayResult) code.CODENO {
	//if order.State != helper.ORDER_STATE_SURE {
	//	return code.ORDER_PAY_REPEAT
	//}
	if order == nil {
		return code.ORDER_NO_ERR
	}

	if strings.Trim(result.SerialNo, " ") == "" {
		return code.ORDER_NO_ERR
	}

	if strings.Trim(result.PlatNo, " ") == " " {
		return code.ORDER_PAY_NO_ERR
	}
	if result.Amount <= 0 {
		return code.ORDER_PAY_AMOUNT_ERR
	}
	return code.SUCCESS
}

// 花费现金余额
func (self *PaymentSrv) Cost(order *app.Order) code.CODENO {
	// 充值数据存储
	rechargeSrv := appsrv.Recharge{}
	rechargeObj := app.Recharge{}
	rechargeObj.UUID = order.UUID
	rechargeObj.Belong = helper.RECGARGE_OF_ORDER
	rechargeObj.BelongID = tips.String(order.OrderNo)
	rechargeObj.Amount = order.Amount
	rechargeSrv.Save(&rechargeObj, &sqlhelper.Where{
		Conds:   []*sqlhelper.CondItem{sqlhelper.WhereCond("Belong", rechargeObj.Belong), sqlhelper.WhereCond("BelongID", rechargeObj.BelongID)},
		Formula: "{1} AND {2}",
	})
	return code.SUCCESS
}

func (self *PaymentSrv) EventErrors(errs []code.CODENO) {
	if len(errs) == 0 {
		return
	}
	logq.InfoF("PAY.PAYMENT.EVENT.ERRORS %v", errs)
}

// functions
